package com.cskaoyan.utils;


import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * 创建日期: 2022/07/25 17:49
 *
 * @author ciggar
 */
public class JDBCUtils2 {

    /** 1. 注册驱动
     *
     *  2. 可配置化
     *
     *  3. 关闭ResultSet
     *
     */


    static Connection connection;


    static {
        try {




            Properties properties = new Properties();
            properties.load(new FileInputStream("jdbc.properties"));

            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driver = properties.getProperty("driver");

            // 注册驱动
//            DriverManager.registerDriver(new Driver());
//            new Driver();
            // 使用反射注册驱动
            Class.forName(driver);


            // 建立连接
            connection = DriverManager.getConnection(url,user,password);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }


    // 获取连接
    public static Connection getConnection(){

        return connection;

    }

    // 关闭资源
    public static void closeResource(Statement statement, Connection connection, ResultSet resultSet){
        try {
            if (resultSet != null) resultSet.close();
            if (statement!= null) statement.close();
            if (connection != null) connection.close();
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
